package com.article.controller;

import com.article.entity.QUsers;
import com.article.service.IQUserService;
import com.article.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.ParseException;

@Controller
@RequestMapping("/api/login")
public class LoginController {

    @Autowired
    private IQUserService iqUserService;

    /**
     * 系统PC平台用户登陆
     * @param request
     * @param response
     * @return
     */
    @CrossOrigin
    @RequestMapping(value="/login")
    @ResponseBody
    public Result login(HttpServletRequest request, HttpServletResponse response){
        String userAccount = request.getParameter("userAccount");
        if(StringUtils.isEmpty(userAccount)){
            return Result.reError("账号不能为空");
        }
        String userPwd =  request.getParameter("userPwd");
        if(StringUtils.isEmpty(userPwd)){
            return Result.reError("密码不能为空");
        }
        String userRole=request.getParameter("userRole");
        if(StringUtils.isEmpty(userRole)){
            return Result.reError("角色不能为空");
        }
        //进行登录认证
        QUsers qUsers=this.iqUserService.login(userAccount,userPwd,userRole);
        if(qUsers==null){
            return Result.reError("账号密码错误！！！");
        }
        request.getSession().setAttribute("user",qUsers);
        return Result.reOk("登录成功");
    }

    /**
     * 注册
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value="/register")
    @ResponseBody
    public Result register(HttpServletRequest request, HttpServletResponse response) throws ParseException {
        String userName = request.getParameter("userName");
        if(StringUtils.isEmpty(userName)){
            return Result.reError("昵称不能为空");
        }
        String userAccount = request.getParameter("userAccount");
        if(StringUtils.isEmpty(userAccount)){
            return Result.reError("账号不能为空");
        }
        String userPwd =  request.getParameter("userPwd");
        if(StringUtils.isEmpty(userPwd)){
            return Result.reError("密码不能为空");
        }
        String userRole=request.getParameter("userRole");
        if(StringUtils.isEmpty(userRole)){
            return Result.reError("角色不能为空");
        }
        String userSex =  request.getParameter("userSex");
        if(StringUtils.isEmpty(userSex)){
            return Result.reError("性别不能为空");
        }
        String userPhone =  request.getParameter("userPhone");
        if(StringUtils.isEmpty(userPhone)){
            return Result.reError("电话不能为空");
        }
        QUsers qUsers=new QUsers();
        qUsers.setUserAccount(userAccount);
        qUsers.setUserPwd(userPwd);
        qUsers.setUserName(userName);
        qUsers.setUserRole(Integer.parseInt(userRole));
        qUsers.setUserSex(userSex);
        qUsers.setUserPhone(userPhone);
        return this.iqUserService.insertDb(qUsers);
    }


    /**
     * 获取当前登录的用户信息
     * @return
     */
    @RequestMapping("/getUserInfo")
    @ResponseBody
    public Result getUserInfo(HttpSession session) {
        QUsers qUsers=(QUsers) session.getAttribute("user");
        return Result.reOk("查询成功",qUsers);
    }


    /**
     * 修改当前登录的用户信息
     * @return
     */
    @RequestMapping("/updateUserInfo")
    @ResponseBody
    public Result updateUserInfo(QUsers qUsers,HttpSession session) {
        this.iqUserService.updatesDb(qUsers);
        //刷新用户
        QUsers db=this.iqUserService.getByIds(qUsers.getId());
        session.setAttribute("user",db);
        return Result.reOk("修改成功");

    }


    /**
     * 修改密码
     * @return
     */
    @RequestMapping("/updatePassWord")
    @ResponseBody
    public Result updatePassWord(HttpSession session,HttpServletRequest request) {
        QUsers qUsers=(QUsers) session.getAttribute("user");
        if(!request.getParameter("userPwd").equals(qUsers.getUserPwd())){
            return Result.reError("密码校验失败");
        }
        if(!request.getParameter("newPwd").equals(request.getParameter("aginPwd"))){
            return Result.reError("两次密码输入不一致");
        }
        qUsers.setUserPwd(request.getParameter("newPwd"));
        return this.iqUserService.updatesDb(qUsers);
    }

    /**
     * 路由地址
     */
    public String prefix = "/view/admin/account";

    /**
     * 进入管理页面
     * @return
     */
    @RequestMapping("/index")
    public String index(){
        return prefix+"/index";
    }


}
